<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */

    /*********************************************************
    *            --== InArch ==--
    *
    * @link http://developer.essentialmind.com/wiki/INA/archs/internals/solutions/web/datatree
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 20.04.2005
    * @copyright essentialmind.com 2005
    *
    *********************************************************/

    /***
     * Unit Test Framework for the Tree Data Handler, using the PEARUnit Package
     *
     * @author Kulikov Alexey <a.kulikov@gmail.com>
     * @link http://pear.php.net/
     * @see /classes/tree.class.php
     * @see /unitTest.inc.php
     ***/
    class treeTest extends PHPUnit_TestCase{        
        protected $tree;

        /**
         * treeTest::__construct()
         * 
         * @param $name
         * @return 
         **/
        function __construct($name){
            $this->PHPUnit_TestCase($name);
        }


        /**
         * treeTest::setUp() - called before the Unit Test is initiated
         * 
         * @return void
         **/
        function setUp(){
            global $db;
            $this->tree = new tree($db,'structure');
        }


        /**
         * treeTest::tearDown() - called after the test is complete
         * 
         * @return void
         **/
        function tearDown(){
            unset($this->tree);
        }
        
        
        /**
         * treeTest::getRandomNode()
         * 
         * @return 
         **/
        private function getRandomNode(){
            global $db;
            return (int)$db->getOne("SELECT id FROM structure ORDER BY random() LIMIT 1");
        }
        
        
        
        /**
         * treeTest::getRootNode()
         * 
         * @return 
         **/
        private function getRootNode(){
            global $db;
            return (int)$db->getOne("SELECT id FROM structure WHERE lft = 1");
        }


        /**
         * treeTest::testAddNode()
         * 
         * adding a node as a child of the root node, in case
         * there is no root node, then one is created and
         * the ability of the class to add a root node is
         * then tested
         * 
         * @return void
         **/
        function testAddNode(){
            //pick some node id at random
            $id = $this->getRootNode();
            
            //is there a valid id?
            if($id > 0){ // yes, we have a tree
                $result = $this->tree->addNode('node'.uniqid(),$id);
            }else{ // no, we are bogus
                $result = $this->tree->addNode();
            }
            $this->assertTrue(is_int($result));
        }
        
        
        /**
         * treeTest::testAddNodeRandom()
         * 
         * adding a child to a node picked at random
         * in case the above test succeeded, then there
         * will be at least one node to play around with
         * thus not having a node fails the test
         * 
         * @return void
         **/
        function testAddNodeRandom(){
            //pick some node id at random
            $id = $this->getRandomNode();
            
            //is there a valid id?
            if($id > 0){ // yes, we have a tree
                $result = $this->tree->addNode('random'.uniqid(),$id);
                $this->assertTrue(is_int($result));

            }else{ // no, there is no random node in the database
                $this->assertTrue(false);
            }
        }
        
        
        /**
         * treeTest::testDropNodeRandom()
         * 
         * Test the dropNode() method of the tree class
         * please read the class's comments for an explanation
         * 
         * @return void
         **/
         /*
          Deprecated, as difficult to emulate, in case
          this test needs to be performed, then uncomment
          and set the values by hand

        function testDropNodeRandom(){
            //pick some node id at random
            $id = $this->getRandomNode();
            
            //is there a valid id?
            if($id > 0){ // yes, we have a tree
                $result = $this->tree->dropNode($id);
                $this->assertTrue($result);
            }else{ // no, there is no tree, thus pass the test
                $this->assertTrue(true);
            }
        }
         */
        
        
        /**
         * treeTest::testGetNodeRandom()
         * 
         * @return void
         **/
        function testGetNodeRandom(){
            //pick some node id from root
            $id = $this->getRandomNode();
            
            //is there a valid id?
            if($id > 0){ //yes, we have a tree
                $result = $this->tree->getNode($id);
                $this->assertTrue(is_array($result));
            }else{ //no, we are bogus
                $this->assertTrue(false);
            }
        }
        
        
        /**
         * treeTest::testGetPath()
         * 
         * Tests the getPath() method of the tree class
         * 
         * @return 
         **/
        function testGetPathRandom(){
            //pick some node id from root
            $id = $this->getRandomNode();
            
            //is there a valid id?
            if($id > 0){ //yes, we have a tree
                $result = $this->tree->getPath($id);
                $this->assertTrue(is_string($result));
            }else{ //no, we are bogus
                $this->assertTrue(false);
            }
        }
        
        
        /**
         * treeTest::testGetTree()
         * 
         * @return void
         **/
        function testGetTree(){
            $result = $this->tree->getTree();
            $this->assertTrue(is_array($result));
        }
        
        
        /**
         * treeTest::testGetTreeFromNode()
         * 
         * @return void
         **/
        function testGetTreeFromNode(){
            //pick some node id from root
            $id = $this->getRandomNode();

            //is there a valid id?
            if($id > 0){ //yes, we have a tree
                $result = $this->tree->getTree($id);
                $this->assertTrue(is_array($result));
            }else{ //no, we are bogus
                $this->assertTrue(false);
            }
        }
        
        
        /**
         * treeTest::testMoveNode()
         * 
         * @return void
         **/

         /*
          Deprecated, as difficult to emulate, in case
          this test needs to be performed, then uncomment
          and set the values by hand

        function testMoveNode(){
            //pick some node id from root
            $id_source = $this->getRandomNode();
            $id_destination = $this->getRandomNode();
            
            //is there a valid id?
            if($id_source > 0 and $id_destination > 0){ //yes, we have a tree
                $result = $this->tree->moveNode($id_source,$id_destination);
                $this->assertTrue($result);
            }else{ //no, we are bogus
                $this->assertTrue(false);
            }
        }
         */
        
        
        /**
         * treeTest::testTraverse()
         * 
         * @return void
         **/
        function testTraverse(){
            $result = $this->tree->traverse();
            $this->assertTrue($result);
        }
    }
?>